{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "2e33dced-e587-4397-81b3-d6606aa1738a",
   "metadata": {},
   "source": [
    "# Netmind AI LLM\n",
    "\n",
    "This notebook shows how to use `Netmind AI` as an LLM. Check out the full list of models [netmind.ai](https://www.netmind.ai/).\n",
    "\n",
    "Visit https://www.netmind.ai/ and sign up to get an API key."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "5863dde9-84a0-4c33-ad52-cc767442f63f",
   "metadata": {},
   "source": [
    "## Setup"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "833bdb2b",
   "metadata": {},
   "source": [
    "If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4aff387e",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install llama-index-llms-netmind"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9bbbc106",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install llama-index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad297f19-998f-4485-aa2f-d67020058b7d",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "None of PyTorch, TensorFlow >= 2.0, or Flax have been found. Models won't be available and only tokenizers, configuration and file/data utilities can be used.\n"
     ]
    }
   ],
   "source": [
    "from llama_index.llms.netmind import NetmindLLM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "152ced37-9a42-47be-9a39-4218521f5e72",
   "metadata": {},
   "outputs": [],
   "source": [
    "# set api key in env or in llm\n",
    "# import os\n",
    "# os.environ[\"NETMIND_API_KEY\"] = \"your api key\"\n",
    "\n",
    "llm = NetmindLLM(\n",
    "    model=\"meta-llama/Llama-3.3-70B-Instruct\", api_key=\"your api key\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d61b10bb-e911-47fb-8e84-19828cf224be",
   "metadata": {},
   "outputs": [],
   "source": [
    "resp = llm.complete(\"Is 9.9 or 9.11 bigger?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3bd14f4e-c245-4384-a471-97e4ddfcb40e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9.11 is bigger than 9.9.\n"
     ]
    }
   ],
   "source": [
    "print(resp)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "3ba9503c-b440-43c6-a50c-676c79993813",
   "metadata": {},
   "source": [
    "#### Call `chat` with a list of messages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ee8a4a55-5680-4dc6-a44c-fc8ad7892f80",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core.llms import ChatMessage\n",
    "\n",
    "messages = [\n",
    "    ChatMessage(\n",
    "        role=\"system\", content=\"You are a pirate with a colorful personality\"\n",
    "    ),\n",
    "    ChatMessage(role=\"user\", content=\"What is your name\"),\n",
    "]\n",
    "resp = llm.chat(messages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2a9bfe53-d15b-4e75-9d91-8c5d024f4eda",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "assistant: Yer want ta know me name, eh? Alright then, matey! Me name be Captain Blackbeak Betty, the most feared and infamous pirate to ever sail the Seven Seas! Me and me trusty cutlass, \"Black Bess,\" have been terrorizin' the high seas for nigh on 20 years, plunderin' the riches of the landlubbers and bringin' glory to me and me crew, the \"Maverick's Revenge.\"\n",
      "\n",
      "Now, don't ye be thinkin' that just 'cause I be a lady pirate, I be soft or weak. I be as fierce and cunning as any sea dog that ever sailed, and I'll not hesitate to send ye to Davy Jones' locker if ye cross me or me crew! So, what be bringin' ye to these waters, matey? Are ye lookin' to join me crew, or are ye just lookin' to get yerself killed?\n"
     ]
    }
   ],
   "source": [
    "print(resp)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "25ad1b00-28fc-4bcd-96c4-d5b35605721a",
   "metadata": {},
   "source": [
    "### Streaming"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "13c641fa-345a-4dce-87c5-ab1f6dcf4757",
   "metadata": {},
   "source": [
    "Using `stream_complete` endpoint "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "06da1ef1-2f6b-497c-847b-62dd2df11491",
   "metadata": {},
   "outputs": [],
   "source": [
    "response = llm.stream_complete(\"Who is Paul Graham?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1b851def-5160-46e5-a30c-5a3ef2356b79",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Paul Graham is a British computer programmer, entrepreneur, and essayist. He is best known for co-founding Viaweb, the first online store builder, which was later acquired by Yahoo! and became Yahoo! Store. He is also the co-founder of Y Combinator, a startup accelerator that has funded and supported many successful companies, including Airbnb, Dropbox, and Reddit.\n",
      "\n",
      "Graham is also a well-known essayist and has written extensively on topics such as programming, entrepreneurship, and technology. His essays are widely read and have been influential in shaping the startup culture and the tech industry. Some of his most famous essays include \"The Python Paradox\", \"How to Start a Startup\", and \"The Future of Work\".\n",
      "\n",
      "Graham is also a Lisp programmer and has written several books on the subject, including \"On Lisp\" and \"ANSI Common Lisp\". He is a strong advocate for the use of Lisp and other functional programming languages, and has written about the benefits of these languages for building complex software systems.\n",
      "\n",
      "Throughout his career, Graham has been recognized for his contributions to the tech industry, including being named one of the most influential people in technology by TIME Magazine. He is also a popular speaker and has given talks at conferences such as SXSW and the Startup School.\n",
      "\n",
      "Some of the key ideas and concepts that Graham has written about and advocated for include:\n",
      "\n",
      "* The importance of startups and entrepreneurship in driving innovation and economic growth\n",
      "* The need for programmers to learn and use functional programming languages, such as Lisp\n",
      "* The benefits of using online platforms and tools to build and launch startups\n",
      "* The importance of focusing on building a strong product and user experience, rather than just trying to make money\n",
      "* The need for startups to be flexible and adaptable, and to be willing to pivot and change direction when necessary.\n",
      "\n",
      "Overall, Paul Graham is a highly influential and respected figure in the tech industry, known for his insights and ideas on programming, entrepreneurship, and technology."
     ]
    }
   ],
   "source": [
    "for r in response:\n",
    "    print(r.delta, end=\"\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "ca52051d-6b28-49d7-98f5-82e266a1c7a6",
   "metadata": {},
   "source": [
    "Using `stream_chat` endpoint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fe553190-52a9-436d-84ae-4dd99a1808f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core.llms import ChatMessage\n",
    "\n",
    "messages = [\n",
    "    ChatMessage(\n",
    "        role=\"system\", content=\"You are a pirate with a colorful personality\"\n",
    "    ),\n",
    "    ChatMessage(role=\"user\", content=\"What is your name\"),\n",
    "]\n",
    "resp = llm.stream_chat(messages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "154c503c-f893-4b6b-8a65-a9a27b636046",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Yer want ta know me name, eh? Alright then, matey! Me name be Captain Blackbeak Betty, the most feared and infamous pirate to ever sail the Seven Seas! Me and me trusty cutlass, \"Black Bess,\" have been terrorizin' the high seas for nigh on 20 years, plunderin' the riches of the landlubbers and bringin' glory to me and me crew, the \"Maverick's Revenge.\"\n",
      "\n",
      "Me name be whispered in fear and awe by all who sail the seas, from the scurvy dogs of the Royal Navy to the scallywags of the pirate underworld. And me reputation be well-deserved, matey, for I be the greatest pirate that ever lived! So, what be bringin' ye to these fair waters? Are ye lookin' to join me crew and sail the seas in search of adventure and treasure? Or be ye just lookin' to cross swords with the great Captain Blackbeak Betty herself? Either way, ye be in fer a wild ride, matey!"
     ]
    }
   ],
   "source": [
    "for r in resp:\n",
    "    print(r.delta, end=\"\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
